package org.erp.product.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.erp.product.domain.dto.TransferDto;
import org.erp.product.domain.dto.TransferLogAddDto;
import org.erp.product.domain.entity.TransferLog;
import org.erp.product.infrastructure.enums.ConnectEventEnum;
import org.erp.product.infrastructure.enums.EventCodeEnum;
import org.erp.product.infrastructure.enums.InspectionEventEnum;
import org.erp.product.infrastructure.enums.RepairEventEnum;
import org.erp.product.infrastructure.mapper.TransferLogMapper;
import org.erp.product.service.TransferLogService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 维修调拨记录服务实现层
 *
 * @author libin
 * @version 1.0
 * @description: 维修调拨记录服务实现层
 * @date 2025/11/13 18:04
 **/
@Log4j2
@Service
@AllArgsConstructor
public class TransferLogServiceImpl extends ServiceImpl<TransferLogMapper, TransferLog> implements TransferLogService {

    @Override
    public void repairSaveBatch(TransferDto transferDto, TransferLogAddDto transferLogAddDto, EventCodeEnum eventCodeEnum, RepairEventEnum repairEventEnum) {
        List<TransferLog> list = new ArrayList<>();
        transferLogAddDto.getRepairInfoList().forEach(r -> {
            TransferLog transferLog = new TransferLog();
            transferLog.setStockId(r.getStockId());
            transferLog.setEventCode(eventCodeEnum.getCode());
            transferLog.setEventName(repairEventEnum.getMessage());
            transferLog.setTransferType(r.getTransferType());
            transferLog.setOperationId(r.getOperationId());
            transferLog.setOperationName(r.getOperationName());
            transferLog.setDeptId(r.getDeptId());
            transferLog.setDeptName(r.getDeptName());
            transferLog.setTransferId(transferDto.getTransferId());
            transferLog.setTransferName(transferDto.getTransferName());
            transferLog.setOldStatus(null == transferLogAddDto.getOldStatus() ? -1 : transferLogAddDto.getOldStatus());
            transferLog.setNewStatus(r.getRepairStatus());
            transferLog.setConnectOldStatus(null == transferLogAddDto.getConnectOldStatus() ? -1 : transferLogAddDto.getConnectOldStatus());
            transferLog.setConnectNewStatus(null == transferLogAddDto.getConnectNewStatus() ? -1 : transferLogAddDto.getConnectNewStatus());
            transferLog.setCreateBy("");
            list.add(transferLog);
        });
        this.baseMapper.saveBatch(list);
    }

    @Override
    public void inspectionSaveBatch(TransferDto transferDto, TransferLogAddDto transferLogAddDto, EventCodeEnum eventCodeEnum, InspectionEventEnum inspectionEventEnum) {
        List<TransferLog> list = new ArrayList<>();
        transferLogAddDto.getProductInfoList().forEach(r -> {
            TransferLog transferLog = new TransferLog();
            transferLog.setStockId(r.getStockId());
            transferLog.setEventCode(eventCodeEnum.getCode());
            transferLog.setEventName(inspectionEventEnum.getMessage());
            transferLog.setTransferType(r.getTransferType());
            transferLog.setOperationId(r.getOperationId());
            transferLog.setOperationName(r.getOperationName());
            transferLog.setDeptId(r.getDeptId());
            transferLog.setDeptName(r.getDeptName());
            transferLog.setTransferId(transferDto.getTransferId());
            transferLog.setTransferName(transferDto.getTransferName());
            transferLog.setOldStatus(null == transferLogAddDto.getOldStatus() ? -1 : transferLogAddDto.getOldStatus());
            transferLog.setNewStatus(r.getInspectionStatus());
            transferLog.setConnectOldStatus(null == transferLogAddDto.getConnectOldStatus() ? -1 : transferLogAddDto.getConnectOldStatus());
            transferLog.setConnectNewStatus(null == transferLogAddDto.getConnectNewStatus() ? -1 : transferLogAddDto.getConnectNewStatus());
            transferLog.setCreateBy("");
            list.add(transferLog);
        });
        this.baseMapper.saveBatch(list);
    }

    @Override
    public void saveBatch(TransferLogAddDto transferLogAddDto, EventCodeEnum eventCodeEnum, ConnectEventEnum connectEventEnum) {
        List<TransferLog> list = new ArrayList<>();
        if (!transferLogAddDto.getRepairInfoList().isEmpty()) {
            transferLogAddDto.getRepairInfoList().forEach(r -> {
                TransferLog transferLog = new TransferLog();
                transferLog.setStockId(r.getStockId());
                transferLog.setEventCode(eventCodeEnum.getCode());
                transferLog.setEventName(connectEventEnum.getMessage());
                transferLog.setTransferType(r.getTransferType());
                transferLog.setOperationId(transferLogAddDto.getOperationId());
                transferLog.setOperationName(transferLogAddDto.getOperationName());
                transferLog.setDeptId(r.getDeptId());
                transferLog.setDeptName(r.getDeptName());
                transferLog.setTransferId(r.getOperationId());
                transferLog.setTransferName(r.getOperationName());
                transferLog.setOldStatus(null == transferLogAddDto.getOldStatus() ? -1 : transferLogAddDto.getOldStatus());
                transferLog.setNewStatus(r.getRepairStatus());
                transferLog.setConnectOldStatus(null == transferLogAddDto.getConnectOldStatus() ? -1 : transferLogAddDto.getConnectOldStatus());
                transferLog.setConnectNewStatus(null == transferLogAddDto.getConnectNewStatus() ? -1 : transferLogAddDto.getConnectNewStatus());
                transferLog.setCreateBy("");
                list.add(transferLog);
            });
        }
        if (!transferLogAddDto.getProductInfoList().isEmpty()) {
            transferLogAddDto.getProductInfoList().forEach(r -> {
                TransferLog transferLog = new TransferLog();
                transferLog.setStockId(r.getStockId());
                transferLog.setEventCode(eventCodeEnum.getCode());
                transferLog.setEventName(connectEventEnum.getMessage());
                transferLog.setTransferType(r.getTransferType());
                transferLog.setOperationId(transferLogAddDto.getOperationId());
                transferLog.setOperationName(transferLogAddDto.getOperationName());
                transferLog.setDeptId(r.getDeptId());
                transferLog.setDeptName(r.getDeptName());
                transferLog.setTransferId(r.getOperationId());
                transferLog.setTransferName(r.getOperationName());
                transferLog.setOldStatus(null == transferLogAddDto.getOldStatus() ? -1 : transferLogAddDto.getOldStatus());
                transferLog.setNewStatus(r.getInspectionStatus());
                transferLog.setConnectOldStatus(null == transferLogAddDto.getConnectOldStatus() ? -1 : transferLogAddDto.getConnectOldStatus());
                transferLog.setConnectNewStatus(null == transferLogAddDto.getConnectNewStatus() ? -1 : transferLogAddDto.getConnectNewStatus());
                transferLog.setCreateBy("");
                list.add(transferLog);
            });
        }
        this.baseMapper.saveBatch(list);
    }
}
